package cn.edu.qzuie.artist.module.td.controller.admin.workscatagory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.workscatagory.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.workscatagory.WorksCatagoryDO;
import cn.edu.qzuie.artist.module.td.service.workscatagory.WorksCatagoryService;

@Tag(name = "管理后台 - TD作品分类")
@RestController
@RequestMapping("/td/works-catagory")
@Validated
public class WorksCatagoryController {

    @Resource
    private WorksCatagoryService worksCatagoryService;

    @PostMapping("/create")
    @Operation(summary = "创建作品分类")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:create')")
    public CommonResult<Long> createWorksCatagory(@Valid @RequestBody WorksCategorySaveReqVO createReqVO) {
        return success(worksCatagoryService.createWorksCatagory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作品分类")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:update')")
    public CommonResult<Boolean> updateWorksCatagory(@Valid @RequestBody WorksCategorySaveReqVO updateReqVO) {
        worksCatagoryService.updateWorksCatagory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:works-catagory:delete')")
    public CommonResult<Boolean> deleteWorksCatagory(@RequestParam("id") Long id) {
        worksCatagoryService.deleteWorksCatagory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:query')")
    public CommonResult<WorksCategoryRespVO> getWorksCatagory(@RequestParam("id") Long id) {
        WorksCatagoryDO worksCatagory = worksCatagoryService.getWorksCatagory(id);
        return success(BeanUtils.toBean(worksCatagory, WorksCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得作品分类列表")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:query')")
    public CommonResult<List<WorksCategoryRespVO>> getWorksCatagoryList(@Valid WorksCategoryListReqVO listReqVO) {
        List<WorksCatagoryDO> list = worksCatagoryService.getWorksCatagoryList(listReqVO);
        return success(BeanUtils.toBean(list, WorksCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作品分类 Excel")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorksCatagoryExcel(@Valid WorksCategoryListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<WorksCatagoryDO> list = worksCatagoryService.getWorksCatagoryList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "作品分类.xls", "数据", WorksCategoryRespVO.class,
                        BeanUtils.toBean(list, WorksCategoryRespVO.class));
    }
    @GetMapping("/list-for-tree")
    @Operation(summary = "获得作品分类列表 for tree")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:query')")
    public  CommonResult<List<WorksCategoryListRespTreeVo>> getWorksCategoryListRespTreeVo(){

        List<WorksCatagoryDO> list=worksCatagoryService.getWorksCatagoryDO();

        return  success(BeanUtils.toBean(list,WorksCategoryListRespTreeVo.class));

    }
    @GetMapping("/getWorksCatagoryDOByParentId")
    @Operation(summary = "通过parentId获得作品分类列表")
    @PreAuthorize("@ss.hasPermission('td:works-catagory:query')")
    public CommonResult<List<WorksCategoryListRespTreeVo>> getWorksCatagoryDOByParentId(Long parentId){
        QueryWrapper<WorksCatagoryDO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<WorksCatagoryDO> list=   worksCatagoryService.list(queryWrapper);

        return  success(BeanUtils.toBean(list,WorksCategoryListRespTreeVo.class));
    }

}